/**
  * Copyright (c) 2015-2016, Javen Zhou  (javenlife@126.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package xin.xinny.weixin.demo;

import com.jfinal.config.*;
import com.jfinal.core.JFinal;
import xin.xinny.kit.JoddPropsKit;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import xin.xinny.log.LogbackFactory;
import xin.xinny.weixin.share.ShareController;
import xin.xinny.weixin.usercontroller.UserController;
import xin.xinny.weixin.weboauth2.RedirectUri;

/**
 * @author Javen
 * 2016年1月15日
 */
public class WeixinConfig extends JFinalConfig {
	
	/**
	 * 如果生产环境配置文件存在，则优先加载该配置，否则加载开发环境配置文件
	 * @param pro 生产环境配置文件
	 * @param dev 开发环境配置文件
	 */
	public void loadProp(String pro, String dev) {
		try {
			JoddPropsKit.use(pro);
		}
		catch (Exception e) {
			JoddPropsKit.use(dev);
		}
	}
	
	public void configConstant(Constants me) {
		JoddPropsKit.use("wx.props");
		JoddPropsKit.use("db.props");
		me.setDevMode(JoddPropsKit.getBoolean("devMode", false));
		me.setEncoding("utf-8");
		me.setViewType(ViewType.JSP);
		me.setLogFactory(new LogbackFactory());
		// ApiConfigKit 设为开发模式可以在开发阶段输出请求交互的 xml 与 json 数据
		ApiConfigKit.setDevMode(me.getDevMode());
	}
	
	public void configRoute(Routes me) {
		me.add("/msg", WeixinMsgController.class);
		me.add("/api", WeixinApiController.class, "/api");
		me.add("/pay", WeixinPayController.class);
		me.add("/", IndexController.class,"/index");
		me.add("/oauth2",RedirectUri.class);
		me.add("/user",UserController.class);
		me.add("/jssdk",ShareController.class,"_front");

	}
	
	public void configPlugin(Plugins me) {
		// C3p0Plugin c3p0Plugin = new C3p0Plugin(JoddPropsKit.get("jdbcUrl"), JoddPropsKit.get("user"), JoddPropsKit.get("password").trim());
		// me.add(c3p0Plugin);
		
		// EhCachePlugin ecp = new EhCachePlugin();
		// me.add(ecp);
		
		// RedisPlugin redisPlugin = new RedisPlugin("weixin", "127.0.0.1");
		// me.add(redisPlugin);
	}
	
	public void configInterceptor(Interceptors me) {
		
	}
	
	public void configHandler(Handlers me) {
		
	}
	
	@Override
	public void configEngine(Engine engine) {
		
	}
	
	public void afterJFinalStart() {
		// 1.5 之后支持redis存储access_token、js_ticket，需要先启动RedisPlugin
//		ApiConfigKit.setAccessTokenCache(new RedisAccessTokenCache());
		// 1.6新增的2种初始化
//		ApiConfigKit.setAccessTokenCache(new RedisAccessTokenCache(Redis.use("weixin")));
//		ApiConfigKit.setAccessTokenCache(new RedisAccessTokenCache("weixin"));
	}

	public static void main(String[] args) {
//		JFinal.start("src/main/webapp", 8080, "/", 5);
	}

}
